package com.gz.logistics.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.library.util.Tools;


public class BaseActivity extends ActionBarActivity {

    protected android.support.v7.app.ActionBar mActionBar;
    private ExitReceiver mExitReceiver;
    protected TextView mActionbarTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mActionBar = getSupportActionBar();
        if(getClass() != LoginActivity.class && getClass() != MainActivity.class ){

            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayHomeAsUpEnabled(true); //给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
            mActionBar.setDisplayShowHomeEnabled(true);//使左上角图标是否显示，如果设成false，则没有程序图标，仅仅就个标题，否则，显示应用程序图标，
                                                       // 对应id为android.R.id.home，对应ActionBar.DISPLAY_SHOW_HOME
            mActionBar.setDisplayUseLogoEnabled(true);

        }else {
            mActionBar.setHomeButtonEnabled(false);
            mActionBar.setDisplayHomeAsUpEnabled(false);
        }
        mActionBar.setDisplayShowTitleEnabled(false); //不显示标题
        mActionBar.setIcon(null); // 不显示图标
        mActionBar.setLogo(R.color.transparent);
        View actionView = getLayoutInflater().inflate(R.layout.action_bar, null);
        mActionbarTitle = (TextView) actionView.findViewById(R.id.tv_actionbar_title);
        mActionBar.setCustomView(actionView, new android.support.v7.app.ActionBar.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));
        mActionBar.setDisplayShowCustomEnabled(true);

        mExitReceiver = new ExitReceiver();
        IntentFilter intentFilter = new IntentFilter(Define.ACTION_EXIT_WHOLE_APP);
        registerReceiver(mExitReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mExitReceiver);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    /**
     * 设置actionbar标题
     * @param title 标题字符串
     * @param hasMenu 是否有actionbar菜单
     */
    public void setActionbarTitle(String title, boolean hasMenu) {
        if(!hasMenu) {
            mActionbarTitle.setPadding(Tools.dip2px(getApplicationContext(), 3), 0, 0, 0);
        }
        mActionbarTitle.setText(title);
    }

    class ExitReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

}

